#include "FunctionInterpreter.h"
#include <qregexp.h>
#include <qdatetime.h>
#include <qdebug.h>

FunctionInterpreter::FunctionInterpreter()
{
}

QVariant FunctionInterpreter::interpretFunction(const QString& func, QVariantList param, bool& bOk) const{
    QVariant result;
    qDebug() << func << param;
    do{
        if(func == "parseDateReg"){
            if(param.size() < 2){
                bOk = false;
                result = QString::fromUtf8("Fonction %1 necessite 2 paramètre").arg(func);
                break;
            }
            QString patern;
            char curChar = -1;
            int countCurChar = 1;
            QString tmp = param.at(1).toString();
            for(int i = 0; i < tmp.size(); i++){
                char c = tmp.at(i).toLatin1();
                if(c == curChar){
                    countCurChar += 1;
                }else if(curChar == -1){
                    curChar = c;
                }
                if(c != curChar || i == tmp.size()-1){
                    switch (curChar) {
                    case 'd':
                    case 'M':
                        if(countCurChar < 3){
                            patern += QString("(\\d{1,%1})").arg(countCurChar);
                        }else{
                            patern += "(\\w+)";
                        }
                        break;
                    case 'h':
                    case 'H':
                    case 'y':
                    case 'm':
                    case 's':
                        patern += QString("(\\d{1,%1})").arg(countCurChar);
                        break;
                    case 'a':
                    case 'A':
                        patern += "(am|pm)";
                        break;
                    default:
                        patern += ".";
                        break;
                    }

                    curChar = c;
                    countCurChar = 1;
                }
            }
            QString txt = param.at(0).toString();
            QRegExp rx(patern, Qt::CaseInsensitive/*, QRegExp::FixedString*/);
            int idx = rx.indexIn(txt);
            int len = 0;
            if(idx != -1){
                len = rx.matchedLength();
            }
            if(idx == -1 || len == 0){
                bOk = false;
                result = QString::fromUtf8("Fonction %1 : impossible de trouver la date dans %2 avec le masque %3 (rx = %4)").arg(func).arg(txt).arg(tmp).arg(patern);
                break;
            }
            txt = txt.mid(idx, len);
            QDateTime dt = QDateTime::fromString(txt, tmp);
            if(!dt.isValid()){
                bOk = false;
                result = QString::fromUtf8("Fonction %1 : la date trouvé dans %2 avec le masque %3 est invalide").arg(func).arg(txt).arg(tmp);
                break;
            }
            result = dt;
            bOk = true;
        }else{
            bOk = false;
            result = QString::fromUtf8("Function Inconnu");
        }
    }while(false);


    return result;
}
